package com.lanqiao.gxnushop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.gxnushop.entity.Glance;
import com.lanqiao.gxnushop.service.GlanceService;
import com.lanqiao.gxnushop.valueObject.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@Tag(name = "GlanceController", description = "浏览管理")
@RestController
public class GlanceController {
    @Autowired
    private GlanceService glanceService;

    @PostMapping("/glance")
    @Operation(summary = "添加浏览记录")
    @Parameter(name = "glance", description = "浏览记录glanceId(自动生成),buyerId(买家Id),productId(商品Id),collect(是否收藏)")
    public boolean addGlance(Glance glance) {
        return glanceService.add(glance);
    }

    @DeleteMapping("/glance/{glanceId}")
    @Operation(summary = "根据浏览Id删除指定浏览记录")
    @Parameter(name = "glanceId", description = "浏览记录glanceId")
    public boolean deleteGlance(@PathVariable String glanceId) {
        return glanceService.delete(glanceId);
    }

    @GetMapping("/glance/selectBuyer/{buyerId}")
    @Operation(summary = "根据买家id查询浏览记录")
    @Parameter(name = "buyerId", description = "买家buyerId")
    public PageInfo<Glance> findByBuyerId(@PathVariable String buyerId, Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Glance> list = glanceService.findByBuyerId(buyerId);
        return new PageInfo<>(list);
    }

    @GetMapping("/glance/selectOne/{glanceId}")
    @Operation(summary = "根据浏览记录id查询浏览记录")
    @Parameter(name = "glanceId", description = "浏览记录glanceId")
    public Glance findById(@PathVariable String glanceId) {
        return glanceService.findById(glanceId);
    }

    @GetMapping("/glance/selectProduct/{productId}")
    @Operation(summary = "根据商品id查询所有浏览记录")
    @Parameter(name = "productId", description = "商品productId")
    public PageInfo<Glance> findByProductId(@PathVariable String productId, Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Glance> list = glanceService.findByProductId(productId);
        return new PageInfo<>(list);
    }

    @GetMapping("/glance/{buyerId}/{productId}")
    @Operation(summary = "根据买家id和商品id查询浏览记录")
    @Parameter(name = "buyerId", description = "买家buyerId")
    @Parameter(name = "productId", description = "商品productId")
    public PageInfo<Glance> findByBuyerIdAndProductId(@PathVariable String buyerId, @PathVariable String productId, Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Glance> list = glanceService.findByBuyerIdAndProductId(buyerId, productId);
        return new PageInfo<>(list);
    }
}
